package com.meida.module.file.provider.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.meida.common.constants.QueueConstants;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.file.client.entity.SysFile;
import com.meida.module.file.provider.oss.AliOssUtil;
import com.meida.module.file.provider.oss.FileUtil;
import com.meida.module.file.provider.service.SysFileService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zyf
 */
@Component
public class UploadRabbitMqListener {

    @Autowired
    private SysFileService fileService;
    @Autowired
    private FileUtil fileUtil;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 更新文件业务ID
     *
     * @return
     */
    @RabbitListener(queues = QueueConstants.QUEUE_UPLOADFILE)
    public void process(EntityMap map) {
        Long busId = map.getLong("busId");
        String entityName = map.get("entityName");
        Long[] fileIds = map.get("fileIds");
        List<EntityMap> files = fileService.selectDeleteFileList(fileIds, busId, entityName);
        if (FlymeUtils.isNotEmpty(files)) {
            for (EntityMap sysFile : files) {
                fileUtil.deleteFile(sysFile.get("localPath"));
                fileService.removeById(sysFile.getLong("fileId"));
            }
        }
        if (FlymeUtils.isNotEmpty(fileIds)) {
            UpdateWrapper u = new UpdateWrapper();
            u.set(true, "busId", busId).set(true, "busType", entityName).in("fileId", fileIds);
            fileService.update(u);
        }
    }

    /**
     * 上传文件到阿里云
     *
     * @return
     */
    /**
     * 上传文件到阿里云
     *
     * @return
     */
    @RabbitListener(queues = QueueConstants.QUEUE_UPLOADOSS)
    public void uploadOss(EntityMap map) {
        Long fileId = map.getLong("fileId");
        String filePath = map.get("filePath");
        String smallPath = map.get("smallPath");
        String fKey = map.get("fKey");
        try {
            if (FlymeUtils.isNotEmpty(filePath)) {
                //上传OSS
                String result = aliOssUtil.aliUpload(filePath, fKey);
                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.eq(true, "fileId", fileId);
                updateWrapper.set(true, "ossPath", result);
                fileService.update(updateWrapper);
            }
            if (FlymeUtils.isNotEmpty(smallPath)) {
                //上传缩略图
                String result = aliOssUtil.aliUpload(smallPath, "small_" + fKey);
                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.eq(true, "fileId", fileId);
                updateWrapper.set(true, "smallOssPath", result);
                fileService.update(updateWrapper);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 删除文件
     *
     * @return
     */
    @RabbitListener(queues = QueueConstants.QUEUE_DELFILE)
    public void delFile(EntityMap map) {
        Long[] busIds = map.getLongs("busIds");
        String entityName = map.get("entityName");
        QueryWrapper q = new QueryWrapper();
        if (FlymeUtils.isNotEmpty(busIds)) {
            q.eq(true, "busType", entityName).in(true, "busId", busIds);
            List<SysFile> fileList = fileService.list(q);
            if (FlymeUtils.isNotEmpty(fileList)) {
                for (SysFile sysFile : fileList) {
                    fileUtil.deleteFile(sysFile.getLocalPath());
                    fileService.removeById(sysFile.getFileId());
                }
            }
        }
    }

}
